package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
<<<<<<< HEAD
import com.itheima.health.dao.MenuDao;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
=======
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Menu;
>>>>>>> 6693e98994950fffdaffe5b5f8491e5db91a2da2
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
<<<<<<< HEAD
import java.util.List;
import java.util.Set;

/**
 * @Author:hujunwei
 * @Description:菜单业务实现类
 * @Date:2019/7/30
 * @Version:1.0
 */
@Service
@Slf4j
@Transactional
=======
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:
 * @data : Created in 2019/7/29
 * @description:  菜单业务实现类
 * @version:1.0
 */
@Service
@Slf4j
>>>>>>> 6693e98994950fffdaffe5b5f8491e5db91a2da2
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

<<<<<<< HEAD
    /*
     * @Author:hujunwei
     * @功能:查询所有菜单，用于新增角色弹窗页面回显
     * @date: 2019/7/30
     * @param
     * @return com.itheima.health.entity.Result
     */
    @Override
    public Result findAll() {
        try {
            List<Menu> menus = menuDao.findAll();
            return new Result(true,"查询菜单列表成功",menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询菜单列表失败");
        }

    }

    /*
     * @Author:hujunwei
     * @功能:通过角色id，查询所有与之对应的菜单ids
     * @date: 2019/7/30
     * @param roleId
     * @return java.lang.Integer[]
     */
    @Override
    public List<Integer> findMenuIdsByRoleId(Integer roleId) {
        Set<Menu> menus = menuDao.findMenuIdsByRoleId(roleId);
        List<Integer> menuIds=null;
        menuIds = new ArrayList<>();//这个不能放在下面的for循环中，不然只会返回一个数据
        for (Menu menu : menus) {
            menuIds.add(menu.getId());
        }
        return menuIds;
=======
    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();


    }

    @Override
    public PageResult pageQuery(QueryPageBean pageBean) {
        // 使用分页插件PageHelper
        PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize());
        // 获取分页数据
        Page<Menu> page = menuDao.selectByCondition(pageBean.getQueryString());
        // 封装分页数据，返回分页对象
        return new PageResult(page.getTotal(), page.getResult());

    }
    @Transactional
    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }
    @Transactional
    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }
    @Transactional
    @Override
    public void deleteById(Integer id) {
        menuDao.deleteMenuById(id);
    }

    /**
     * 根据用户名获取对应菜单
     * @param name
     * @return
     */
    @Override
    public List<Map<String,Object>> getMenuBy(String name) {
        //首先根据用户名密码 获取用户Id
        List<Map<String,Object>> menus=new ArrayList<>();
        List<Menu> menuList = menuDao.getMenuByUserName(name);
        log.debug("<<<<<  listMap:{}",menuList);
        //遍历菜单，
        for (Menu menu : menuList) {
            Integer id = menu.getId();
            //根据菜单 父节点id，查到对应子菜单
            List<Menu> child = menuDao.findChildren(id);
            if (child.size() == 0){  // 不能写child==null，child是list集合
                // 不作任何处理
            }else {

                HashMap<String, Object> map = new HashMap<>();
                map.put("path",menu.getPath());
                map.put("title",menu.getName());
                // map.put("linkUrl",menu.getLinkUrl());
                map.put("icon",menu.getIcon());

                //创建一个list集合接收children
                List<Map<String,Object>> childMenus=new ArrayList<>();
                for (Menu childMenu : child) {
                    HashMap<String, Object> childMap = new HashMap<>();
                    childMap.put("path",childMenu.getPath());
                    childMap.put("title",childMenu.getName());
                    childMap.put("linkUrl",childMenu.getLinkUrl());

                    childMenus.add(childMap);
                }

                map.put("children",childMenus);
                menus.add(map);
            }
        }
        return menus;
>>>>>>> 6693e98994950fffdaffe5b5f8491e5db91a2da2
    }
}
